package com.sjgtw.cloud.web.freeze.service.impl;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.freeze.domain.FreezeRestore;
import com.sjgtw.cloud.web.freeze.mapper.FreezeRestoreMapper;
import com.sjgtw.cloud.web.freeze.service.IFreezeRestoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 冻害-恢复情况Service业务层处理
 * 
 * @author lvyoufang
 * @date 2024-03-17
 */
@Service
public class FreezeRestoreServiceImpl implements IFreezeRestoreService 
{
    @Autowired
    private FreezeRestoreMapper freezeRestoreMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询冻害-恢复情况
     * 
     * @param id 冻害-恢复情况主键
     * @return 冻害-恢复情况
     */
    @Override
    public FreezeRestore selectFreezeRestoreById(Long id)
    {
        return freezeRestoreMapper.selectFreezeRestoreById(id);
    }

    /**
     * 查询冻害-恢复情况列表
     * 
     * @param freezeRestore 冻害-恢复情况
     * @return 冻害-恢复情况
     */
    @Override
    public List<FreezeRestore> selectFreezeRestoreList(FreezeRestore freezeRestore)
    {
        return freezeRestoreMapper.selectFreezeRestoreList(freezeRestore);
    }

    /**
     * 新增冻害-恢复情况
     * 
     * @param freezeRestore 冻害-恢复情况
     * @return 结果
     */
    @Override
    public int insertFreezeRestore(FreezeRestore freezeRestore)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        freezeRestore.setCreateUserId(user.getUserid());
        freezeRestore.setCreateBy(user.getUsername());
        freezeRestore.setCreateTime(DateUtils.getNowDate());
        return freezeRestoreMapper.insertFreezeRestore(freezeRestore);
    }

    /**
     * 修改冻害-恢复情况
     * 
     * @param freezeRestore 冻害-恢复情况
     * @return 结果
     */
    @Override
    public int updateFreezeRestore(FreezeRestore freezeRestore)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        freezeRestore.setUpdateBy(user.getUsername());
        freezeRestore.setUpdateTime(DateUtils.getNowDate());
        return freezeRestoreMapper.updateFreezeRestore(freezeRestore);
    }

    /**
     * 批量删除冻害-恢复情况
     * 
     * @param ids 需要删除的冻害-恢复情况主键
     * @return 结果
     */
    @Override
    public int deleteFreezeRestoreByIds(String[] ids)
    {
        return freezeRestoreMapper.deleteFreezeRestoreByIds(ids);
    }

    /**
     * 删除冻害-恢复情况信息
     * 
     * @param id 冻害-恢复情况主键
     * @return 结果
     */
    @Override
    public int deleteFreezeRestoreById(Long id)
    {
        return freezeRestoreMapper.deleteFreezeRestoreById(id);
    }

    @Override
    public void deleteByFreezeId(Long freezeId) {
        freezeRestoreMapper.deleteByFreezeId(freezeId);
    }

    @Override
    public void batchSave(List<FreezeRestore> entities) {
        if (entities != null && entities.size() > 0) {
            if (entities.size() >= 200) {
                int index = 0;
                while (index <= entities.size()) {
                    int last = Math.min(index + 200, entities.size());
                    List<FreezeRestore> temp = entities.subList(index, last );
                    if (temp != null && temp.size() > 0) {
                        freezeRestoreMapper.batchSave(temp);
                    }
                    index += 200;
                }
            } else {
                freezeRestoreMapper.batchSave(entities);
            }
        }
    }

    @Override
    public List<FreezeRestore> selectByFreezeIds(List<Long> freezeIds) {
        return freezeRestoreMapper.selectByFreezeIds(freezeIds);
    }

}
